﻿using AutoMapper;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using WMS.FC.Application.System;
using WMS.FC.Application.User.Interface;
using WMS.FC.Domain.DataTransferObject.User;
using WMS.FC.Domain.DomainObject.System;
using WMS.FC.Domain.Repository;
using WMS.FC.Domain.Service.User.Interface;
using WMS.FC.Domain.ValueObject.User;
using WMS.FC.Infrastructure.Common;

namespace WMS.FC.Application.User
{
    public class UserService : ServiceBase, IUserService
    {
        protected readonly ICheckUserLoginService checkUserLoginService;

        public UserService(
            ILogger<ServiceBase> logger,
            IOptions<WmsSettings> options,
            IMapper mapper,
            IUnitOfWork unitOfWork,
            ICheckUserLoginService checkUserLoginService)
            : base(logger, options, mapper, unitOfWork)
        {
            this.checkUserLoginService = checkUserLoginService;
        }

        public async Task<ResponseBase> CheckUserLogin(CheckUserLoginDTO dTO)
        {
            var response = new ResponseBase();
            try
            {
                var checkUserLoginResult = await checkUserLoginService.CheckUserLogin(dTO.UserName, EncryptHelper.DecodeBase64("utf-8", dTO.Password), dTO.UserInfo);
                response = response.TransferResult(checkUserLoginResult, mapper.Map<UserVO>);
            }
            catch (Exception ex)
            {
                return response.Exception(ex);
            }

            return response;
        }

        public async Task<ResponseBase> ChangePassword(ChangePwdDTO dTO)
        {
            var response = new ResponseBase();
            try
            {
                var changePwdResult = await checkUserLoginService.ChangePassword(dTO.UserName, dTO.Password, EncryptHelper.Encrypt(dTO.NewPassword), dTO.UserInfo);
                response = response.TransferResult(changePwdResult);
            }
            catch (Exception ex)
            {
                return response.Exception(ex);
            }

            return response;
        }
    }
}
